//
//  BSZSecrectViewController.m
//  BuSuanZi
//
//  Created by staff on 3/15/25.
//

#import "BSZLoginSecrectViewController.h"
#import "BSZLoginCodeViewController.h"

static const NSString *identifer = @"BSZLoginSecrectViewController";


@interface BSZLoginSecrectViewController ()<UITextViewDelegate>

@property (nonatomic, strong) UITextField *phoneTextFiled;
@property (nonatomic, strong) UITextField *codeTextFiled;
@property (nonatomic, strong) UIButton   *agreeButton;

@end

@implementation BSZLoginSecrectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化
    [self stepTableView];
}

#pragma mark -- 初始化列表
- (void)stepTableView{
    self.dataTableView.separatorColor = ClearColor;
    self.dataTableView.backgroundColor = WhiteColor;
    self.dataTableView.bounces = YES;
    self.dataTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self.view addSubview:self.dataTableView];
}

#pragma mark -- 验证码登录
- (void)leftAction{
    BSZLoginCodeViewController *Vc = [[BSZLoginCodeViewController alloc] init];
    [self.navigationController pushViewController:Vc animated:YES];
}

#pragma mark -- 注册
- (void)rightAction{
    
}

#pragma mark --同意协议
- (void)agreeAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    
    sender.selected ? [sender setImage:[UIImage imageNamed:@"椭圆形"] forState:UIControlStateNormal] : [sender setImage:[UIImage imageNamed:@"椭圆形"] forState:UIControlStateNormal];
}

//键盘消失
- (void)dismissKeyBoardAction{
    [self.phoneTextFiled resignFirstResponder];
    [self.codeTextFiled resignFirstResponder];
}

#pragma mark -- 协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return SCREEN_WIDTH*2./3.;
    }else if (indexPath.row == 5){
        return SCREEN_WIDTH;
    }else if (indexPath.row == 3){
        return PPMUIRealValue(CELLHEIGHT);
    }else if (indexPath.row == 4){
        return PPMUIRealValue(80);
    }else{
        return PPMUIRealValue(65);
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identify = @"login";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (cell) {
        [cell removeFromSuperview];
        cell = nil;
    }
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.row == 0) {
        UILabel *signLabel = [BSZUIKit creatLabelWithFrame:CGRectMake(PPMUIRealValue(30), SCREEN_WIDTH/3., SCREEN_WIDTH-PPMUIRealValue(60), SCREEN_WIDTH/3.) text:@"你好\n欢迎使用!" font:SystemFont(26) textColor:DefaultBlackColor textAlignment:NSTextAlignmentLeft];
        [cell.contentView addSubview:signLabel];
    }else if (indexPath.row == 1){
        UIView *_elementView = [[UIView alloc]initWithFrame:CGRectMake(PPMUIRealValue(30), 0, SCREEN_WIDTH-PPMUIRealValue(60), PPMUIRealValue(45))];
        [_elementView setAllCorner:_elementView.height/2.0];
        _elementView.backgroundColor = ColdLightGrayColor;
        [cell.contentView addSubview:_elementView];
        
        UIImageView *prevImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _elementView.height, _elementView.height)];
        [_elementView addSubview:prevImageView];
        
        self.phoneTextFiled.frame = CGRectMake(prevImageView.right+PPMUIRealValue(10), 0, _elementView.width-PPMUIRealValue(70), _elementView.height);
        [_elementView addSubview:self.phoneTextFiled];
        
        UIView *underLineView = [[UIView alloc]initWithFrame:CGRectMake(0, _elementView.height-1, _elementView.width, 1)];
        underLineView.backgroundColor = ColdLightGrayColor;
        [_elementView addSubview:underLineView];
    }else if (indexPath.row == 2){
        UIView *_elementView = [[UIView alloc]initWithFrame:CGRectMake(PPMUIRealValue(30), 0, SCREEN_WIDTH-PPMUIRealValue(60), PPMUIRealValue(45))];
        [_elementView setAllCorner:_elementView.height/2.0];
        _elementView.backgroundColor = ColdLightGrayColor;
        [cell.contentView addSubview:_elementView];
                
        UIImageView *prevImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _elementView.height, _elementView.height)];
        [_elementView addSubview:prevImageView];
        
        self.codeTextFiled.frame = CGRectMake(prevImageView.right + PPMUIRealValue(10), 0, _elementView.width - PPMUIRealValue(70), _elementView.height);
        [_elementView addSubview:self.codeTextFiled];
        
        UIView *underLineView = [[UIView alloc]initWithFrame:CGRectMake(0, _elementView.height-1, _elementView.width, 1)];
        underLineView.backgroundColor = ColdLightGrayColor;
        [_elementView addSubview:underLineView];
    }else if (indexPath.row == 3){
        NSArray *imgArry = @[@"验证码登录",@"还没注册，去注册？"];
        for (NSInteger i=0; i<imgArry.count; i++) {
            BSZButton *buttonx = [BSZButton buttonWithType:UIButtonTypeSystem];
            [buttonx setTitle:imgArry[i] forState:UIControlStateNormal];
            [buttonx setTitleColor:DefaultBlackColor forState:UIControlStateNormal];
            // [buttonx setBackgroundColor:RedColor];
            buttonx.titleLabel.font = SystemFont(11.);
            if (i == 0) {
                buttonx.titleLabel.textAlignment = NSTextAlignmentLeft;
                buttonx.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
                [buttonx setTitleColor:DefaultColor forState:UIControlStateNormal];
                [buttonx addTarget:self action:@selector(leftAction) forControlEvents:UIControlEventTouchUpInside];
                buttonx.frame = CGRectMake(PPMUIRealValue(40), PPMUIRealValue(10), PPMUIRealValue(80), PPMUIRealValue(30));
            }else{
                buttonx.titleLabel.textAlignment = NSTextAlignmentRight;
                buttonx.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
                [buttonx addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
                buttonx.frame = CGRectMake(SCREEN_WIDTH-PPMUIRealValue(200), PPMUIRealValue(10), PPMUIRealValue(160), PPMUIRealValue(30));
            }
            [cell.contentView addSubview:buttonx];
        }
    }else if (indexPath.row == 4){
        UIButton *loginButton = [BSZUIKit initTextWithFrame:CGRectMake(PPMUIRealValue(30), 0, SCREEN_WIDTH-PPMUIRealValue(60), PPMUIRealValue(50)) titleForButton:@"登录" titleColorForButton:WhiteColor fontForButton:SystemFont(15.) backgroundColorForButton:DefaultColor];
        [loginButton setAllCorner:loginButton.height/2.];
        [loginButton addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:loginButton];
        

    }else {
        
        
//        self.agreeButton.frame = CGRectMake(PPMUIRealValue(30), PPMUIRealValue(200), PPMUIRealValue(20), PPMUIRealValue(20));
//        [cell.contentView addSubview:self.agreeButton];
        
        //内容文本
        NSString *content = @"注册即代表你同意《用户协议》《隐私政策》";
        CGFloat W = [[Global shareInstance] widthForText:content withFont:SystemFont(10) withHeight:PPMUIRealValue(20)]+PPMUIRealValue(20);
        UITextView *contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2.-W/2., PPMUIRealValue(200), W, PPMUIRealValue(20))];
        contentTextView.backgroundColor = ClearColor;
        contentTextView.attributedText = [self getContentLabelAttributedText:content withTextArry:@[@"用户协议", @"隐私政策"] withProtocalArry:@[@"userProtocol://", @"userPolicy://"]];
        contentTextView.textAlignment = NSTextAlignmentCenter;
        contentTextView.delegate = self;
        contentTextView.editable = NO;        //必须禁止输入，否则点击将弹出输入键盘
        contentTextView.scrollEnabled = NO;
        [cell.contentView addSubview:contentTextView];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self dismissKeyBoardAction];
}

#pragma Others -- 属性
- (NSAttributedString *)getContentLabelAttributedText:(NSString *)text withTextArry:(NSArray *)textArry withProtocalArry:(NSArray *)protocalArry{
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:SystemFont(10),NSForegroundColorAttributeName:DefaultBlackColor}];
    
    for (NSInteger i=0; i<textArry.count; i++) {
        NSString *txt = textArry[i];
        NSString *protocal = protocalArry[i];
        
        NSRange range = [text rangeOfString:txt];
        [attrStr addAttribute:NSForegroundColorAttributeName value:DefaultColor range:range];
        [attrStr addAttribute:NSLinkAttributeName value:protocal range:range];
    }
    return attrStr;
}

#pragma mark --懒加载
- (UITextField *)phoneTextFiled{
    if (!_phoneTextFiled) {
        _phoneTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(PPMUIRealValue(10), PPMUIRealValue(10), SCREEN_WIDTH-PPMUIRealValue(20), PPMUIRealValue(CELLHEIGHT))];
        _phoneTextFiled.placeholder = @"请输入手机号";
        _phoneTextFiled.font = SystemFont(13);
        _phoneTextFiled.textColor = DefaultBlackColor;
        _phoneTextFiled.keyboardType = UIKeyboardTypeNumberPad;
        _phoneTextFiled.clearButtonMode = UITextFieldViewModeAlways;
    }
    return _phoneTextFiled;
}
- (UITextField *)codeTextFiled{
    if (!_codeTextFiled) {
        _codeTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(PPMUIRealValue(10), PPMUIRealValue(10), SCREEN_WIDTH-PPMUIRealValue(120), PPMUIRealValue(CELLHEIGHT))];
        _codeTextFiled.placeholder = @"请输入密码";
        _codeTextFiled.font = SystemFont(13);
        _codeTextFiled.clearButtonMode = UITextFieldViewModeAlways;
        _codeTextFiled.textColor = DefaultBlackColor;
        _codeTextFiled.keyboardType = UIKeyboardTypeASCIICapable;
        _codeTextFiled.secureTextEntry = YES;
    }
    return _codeTextFiled;
}
- (UIButton *)agreeButton{
    if (!_agreeButton) {
        _agreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_agreeButton setImage:[UIImage imageNamed:@"椭圆形"] forState:UIControlStateNormal];
        [_agreeButton addTarget:self action:@selector(agreeAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _agreeButton;
}


@end
